package com.cameron.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cameron.service.MsmService;
import com.google.gson.Gson;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class MsmServiceImpl implements MsmService {


    @Override
    public boolean sendMessage(String phone, HashMap<String, Object> param) {
        if (0 == 1){
            DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAI5tPfQ1vGqhfNqenFvwMP", "Kr2S97ghO7ALqM4qtW2LlCW7pJi2VR");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            //设置发送相关的参数
            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", "验证码短信");
            request.putQueryParameter("TemplateCode", "SMS_244290290");
            request.putQueryParameter("TemplateParam",
                    JSONObject.toJSONString(param));

            try {
                CommonResponse response = client.getCommonResponse(request);
                return response.getHttpResponse().isSuccess();
            } catch (ServerException e) {
                e.printStackTrace();
                log.error(e.getErrMsg());
                return false;
            } catch (ClientException e) {
                e.printStackTrace();
                log.error("ErrCode:" + e.getErrCode());
                log.error("ErrMsg:" + e.getErrMsg());
                log.error("RequestId:" + e.getRequestId());
                return false;
            }
        }

      /*  Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId("LTAI5tPfQ1vGqhfNqenFvwMP")
                // 您的 AccessKey Secret
                .setAccessKeySecret("Kr2S97ghO7ALqM4qtW2LlCW7pJi2VR");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = null;
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("验证码短信")
                .setTemplateCode("SMS_244290290")
                .setPhoneNumbers(phone)
                .setTemplateParam(JSONObject.toJSONString(param));
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info(response.getBody().getMessage());
        } catch (TeaException error) {
            // 如有需要，请打印 error
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
            return false;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            log.error(com.aliyun.teautil.Common.assertAsString(error.message));
            return false;
        }*/

        return true;
    }

}
